package tt.love.hw.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.commons.beanutils.BeanUtils;
import org.datanucleus.util.StringUtils;

import tt.love.hw.PMF;
import tt.love.hw.jdo.Application;


public class ApplicationService {

	@SuppressWarnings("unchecked")
	public List<Application> getApplication(Application app) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(Application.class);
			String userId = app.getRequestUserId();
			String status = app.getStatus();
			Map<String, String> filterMap = new HashMap<String, String>();
			query.setFilter("status == sStatus && requestUserId == sUserId");
			query.declareParameters("String sStatus,String sUserId");
			filterMap.put("sStatus", status);
			filterMap.put("sUserId", userId);
			List<Application> resultApplication = (List<Application>) query
					.executeWithMap(filterMap);
			//pm.detachCopyAll(resultApplication);
			List<Application> appListWithoutObjectManager = new ArrayList<Application>();
			for(Application oldApp:resultApplication){
				Application newApp =  new Application();
				try {
					BeanUtils.copyProperties(newApp, oldApp);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				appListWithoutObjectManager.add(newApp);
			}
			return appListWithoutObjectManager;
//			return resultApplication;
		} finally {
				pm.close();
		}
	}

	public List<Application> getApplicationByUserIdAndStatus(String userId,
			String status)  {
		Application app = new Application();
		app.setRequestUserId(userId);
		app.setStatus(status);
		return getApplication(app);
	}
	
	public List<Application> getPendingApplication(String userId) {
	   	return getApplicationByUserIdAndStatus(userId,"Pending");
	}	
	public List<Application> getApproveApplication(String userId) {
	   	return getApplicationByUserIdAndStatus(userId,"Approve");
	}
	public List<Application> getRejectApplication(String userId) {
	   	return getApplicationByUserIdAndStatus(userId,"Reject");
	}
	public List<Application> getCancelApplication(String userId){
	   	return getApplicationByUserIdAndStatus(userId,"Cancel");
	}
	
	public void updateNewCommentBy(String userId,String appId){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Application app = pm.getObjectById(Application.class, Long
					.valueOf(appId));
			app.setNewCommentBy(userId);
		} finally {
			pm.close();
		}
	}
	
	public void removeNewCommentBy(String userId,String appId){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Application app = pm.getObjectById(Application.class, Long
					.valueOf(appId));
			if(!StringUtils.isEmpty(app.getNewCommentBy())&& !app.getNewCommentBy().equals(userId)){
			     app.setNewCommentBy(null);
			}
		} finally {
			pm.close();
		}
	}

}
